package com.SHA1604.tiger.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.SHA1604.tiger.R;
import com.SHA1604.tiger.bean.DailyRecommandBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by cay on 2016/7/27.
 */
public class RecommandAdapter extends BaseAdapter {
    private Context mContext;
    private List<DailyRecommandBean.DataBean> mData;

    public RecommandAdapter(Context context, List<DailyRecommandBean.DataBean> data) {
        mContext = context;
        mData = data;
    }
    private final int TYPE_SINGLE = 0;
    private final int TYPE_DING = 1;
    //private final int TYPE_MULTI = 2;
    private final int TYPE_COUNT = TYPE_DING+1;

    @Override
    public int getItemViewType(int position) {
        String type = mData.get(position).getShow_type();
        boolean zhiding = mData.get(position).getData().isZhiding();
        if ("single1".equals(type)) {
            if (zhiding) {
                return TYPE_DING;
            }
           // return TYPE_SINGLE;
        }
//        }else if ("multi2".equals(type)){
//            return TYPE_MULTI;
//        }
        return TYPE_SINGLE;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    class ZhiDingHolder{
        private ImageView iv_ding;
        private TextView tv_title,tv_subtitle;

        public ZhiDingHolder(View convertView) {
            iv_ding = (ImageView) convertView.findViewById(R.id.iv_ding);
            tv_title = (TextView) convertView.findViewById(R.id.tv_ding_title);
            tv_subtitle = (TextView) convertView.findViewById(R.id.tv_ding_subtitle);
        }
    }

    class SingleHolder{
        private ImageView iv_single;
        private TextView tv_single_title,
                tv_single_subtitle,tv_single_column_name,tv_single_merchant,tv_single_hits;

        public SingleHolder(View convertView) {
            iv_single = (ImageView) convertView.findViewById(R.id.iv_single);
            tv_single_title = (TextView) convertView.findViewById(R.id.tv_single_title);
            tv_single_subtitle = (TextView) convertView.findViewById(R.id.tv_single_subtitle);
            tv_single_column_name = (TextView) convertView.findViewById(R.id.tv_single_column_name);
            tv_single_merchant = (TextView) convertView.findViewById(R.id.tv_single_merchant);
            tv_single_hits = (TextView) convertView.findViewById(R.id.tv_single_hits);
        }
    }

//    class MuitlHolder{
//        private ImageView iv1,iv2,iv3;
//        private CircleImageView civ;
//        private TextView tv_muitl_title,tv_hits;
//
//        public MuitlHolder(View convertView) {
//            iv1 = (ImageView) convertView.findViewById(R.id.iv_multi_iv1);
//            iv2 = (ImageView) convertView.findViewById(R.id.iv_multi_iv2);
//            iv3 = (ImageView) convertView.findViewById(R.id.iv_multi_iv3);
//            civ = (CircleImageView) convertView.findViewById(R.id.civ_multi);
//            tv_muitl_title = (TextView) convertView.findViewById(R.id.tv_multi);
//            tv_hits = (TextView) convertView.findViewById(R.id.tv_multi_hits);
//        }
//    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //得到当前位置类型
        int itemViewType = getItemViewType(position);
        ZhiDingHolder zhiDingHolder = null;
        SingleHolder singleHolder = null;
      //  MuitlHolder muitlHolder = null;

        if (convertView == null){
            switch (itemViewType){
                case TYPE_DING:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.recommand_item_ding,parent,false);
                    zhiDingHolder = new ZhiDingHolder(convertView);
                    convertView.setTag(zhiDingHolder);
                    break;
                case TYPE_SINGLE:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.recommand_item_single,parent,false);
                    singleHolder = new SingleHolder(convertView);
                    convertView.setTag(singleHolder);
                    break;
//                case TYPE_MULTI:
//                    convertView = LayoutInflater.from(mContext).inflate(R.layout.recommand_item_multi,parent,false);
//                    muitlHolder = new MuitlHolder(convertView);
//                    convertView.setTag(muitlHolder);
//                    break;
            }
        }else{
            switch (itemViewType){
                case TYPE_DING:
                    zhiDingHolder = (ZhiDingHolder) convertView.getTag();
                    break;
                case TYPE_SINGLE:
                    singleHolder = (SingleHolder) convertView.getTag();
                    break;
//                case TYPE_MULTI:
//                    muitlHolder = (MuitlHolder) convertView.getTag();
//                    break;
            }
        }
        DailyRecommandBean.DataBean.DataBean2 data = mData.get(position).getData();
        switch (itemViewType){
            case TYPE_DING:
                Picasso.with(mContext).load(data.getImg()).config(Bitmap.Config.RGB_565).into(zhiDingHolder.iv_ding);
                zhiDingHolder.tv_title.setText(data.getTitle());
                zhiDingHolder.tv_subtitle.setText(data.getSubtitle());
                break;
            case TYPE_SINGLE:
                Picasso.with(mContext).load(data.getImg()).config(Bitmap.Config.RGB_565).into(singleHolder.iv_single);
                singleHolder.tv_single_title.setText(data.getTitle());
                singleHolder.tv_single_subtitle.setText(data.getSubtitle());
                singleHolder.tv_single_column_name.setText(data.getColumn_name());
                singleHolder.tv_single_merchant.setText(data.getMerchant());
                singleHolder.tv_single_hits.setText(data.getHits());
                break;
//            case TYPE_MULTI:
//                Picasso.with(mContext).load(data.)
//                break;
        }
        return convertView;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}
